﻿using System;
using System.Collections.Generic;
using System.Text;
using LogQuery = MSUtil.LogQueryClassClass;
using IISW3CInputFormat = MSUtil.COMIISW3CInputContextClassClass;
using SQLOutput = MSUtil.COMSQLOutputContextClassClass;
using LogRecordSet = MSUtil.ILogRecordset;

namespace LogBase
{
    public class LogParserApi
    {
        #region Attributes

        private LogQuery oLogQuery = new LogQuery();
        private IISW3CInputFormat oIISW3CInputFormat = new IISW3CInputFormat();
        private SQLOutput oSQLOutput = new SQLOutput();

        private string sql = "SELECT logfilename,'{3}' as sitename,'{4}' as ldate,c-ip as clientip, cs-uri-stem as url,count(*) as Recordcount,'' as iparea,'' as ipaddress,'' as comment,'{5}' as stype INTO QT_Analy_temp FROM {0} WHERE TO_LOWERCASE (EXTRACT_EXTENSION(cs-uri-stem)) {1} IN ('{2}') group by logfilename,clientip,url";

        #endregion

        #region 构造函数

        public LogParserApi()
        {
        }

        #endregion

        #region Method

        public void InitSQLServer(string server,string database,string uid,string pass)
        {
            oSQLOutput.createTable = true;
            oSQLOutput.driver = "SQL Server";
            oSQLOutput.server = server;
            oSQLOutput.database = database;
            oSQLOutput.username = uid;
            oSQLOutput.password = pass;
        }

        public bool Execute(string logfile, string type, List<string> filters, string guid, string date, string stype)
        {
            return oLogQuery.ExecuteBatch(string.Format(sql, logfile, type, string.Join("';'", filters.ToArray()), guid, date, stype), oIISW3CInputFormat, oSQLOutput);
        }

        #endregion

    }
}
